# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.

pkgname=bluemaho
pkgver=090417
pkgrel=5
pkgdesc="A GUI-shell (interface) for suite of tools for testing security of bluetooth devices"
arch=('x86_64' 'aarch64')
url="http://wiki.thc.org/BlueMaho"
license=('GPL3')
depends=('wxpython' 'bluediving' 'bss' 'bt-audit' 'hidattack' 'glib2')
groups=('blackarch' 'blackarch-wireless')
source=("http://wiki.thc.org/BlueMaho?action=AttachFile&do=get&target=bluemaho_v$pkgver.tgz")
sha512sums=('216db6f836b20b7f70f3a56bec95ab0a607a2af5')

prepare() {
  cd "$pkgname-$pkgver/exploits/sources/bluepimp"

  sed -i 's:hci_remote_name:hci_read_remote_name:' obex_socket.c

  cd "$srcdir/bluemaho-$pkgver/exploits/sources/helomoto"

  sed -i "s:gcc :gcc $CFLAGS :g" Makefile
}

build() {
  cd "$pkgname-$pkgver/tools"

  chmod -R -x *
  chmod -R +X *
  find . -name '*.sh' -o -name '*.py' -exec chmod +x '{}' \;

  cd replay_packet
  chmod -x *
  chmod +x *.sh

  install -dm 755 "$pkgdir/usr/share/$pkgname/tools"

  cd "$pkgdir/usr/share/$pkgname/tools"

  for tool in 'atshell' 'bccmd' 'psm_scan' 'rfcomm_scan' 'bss' 'btobex' \
              'carwhisperer' 'l2cap-packet' 'redfang' 'ussp-push'; do
    ln -sf "/usr/bin/$tool" .
  done

  cd "$srcdir/$pkgname-$pkgver/tools"
  rm -rf sources/
  cp -r * "$pkgdir/usr/share/$pkgname/tools"

  # Symlinks available exploits
  install -dm 755 "$pkgdir/usr/share/$pkgname/exploits/"
  cd "$pkgdir/usr/share/$pkgname/exploits/"

  ln -sf /usr/bin/hidattack .
  ln -sf /usr/share/hidattack/ha.inp hidattack.inp

  cd "$srcdir/$pkgname-$pkgver/exploits"
  chmod -x *

  # Build other exploits
  msg "Building bluepimp"
  cd sources/bluepimp
  make CFLAGS="$CFLAGS" && mv ussp-push ../../bluepimp
  cd ../..

  msg "Building hcidump-dos"
  cd sources/
  gcc -lbluetooth $CFLAGS bluez_hcidump_v129_dos.c -o bluez_hcidump_v129_dos
  mv bluez_hcidump_v129_dos ..
  cd ..

  msg "Building helomoto"
  cd sources/helomoto/
  make CFLAGS="$CFLAGS"
  mv helomoto ../..
  cd ../..

  msg "Building nokiaN70_l2cap_packet_dos"
  cd sources/
  gcc -lbluetooth $CFLAGS nokiaN70_l2cap_packet_dos.c -o nokiaN70_l2cap_packet_dos
  mv nokiaN70_l2cap_packet_dos ..
  cd ..

  msg "Building sonyericsson_reset_display"
  cd sources/
  gcc -lbluetooth $CFLAGS sonyericsson_reset_display.c -o sonyericsson_reset_display
  mv sonyericsson_reset_display ..
  cd ..
}

package() {
  chmod +x opush_abuse.sh mod3abuse.py
  rm hidattack*

  rm -rf sources/
  cp * "$pkgdir/usr/share/bluemaho/exploits/"

  cd ../config
  rm build.sh
  chmod -x *
  chmod +x themes themes/maho
  cd themes/maho
  chmod -x *
  cd ../../../

  cp -r bluemaho.py config "$pkgdir/usr/share/$pkgname/"
}
